package com.rock.drawboard.socket;

import com.rock.drawboard.constant.Config;
import com.rock.drawboard.constant.EventConfig;
import com.rock.drawboard.event.BoardEvent;
import com.rock.drawboard.event.LoginEvent;
import com.rock.drawboard.module.Client;
import com.rock.drawboard.module.DataPackage;
import com.rock.drawboard.utils.LogUtils;

import org.greenrobot.eventbus.EventBus;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created by 石昌宜 on 2017/3/20.
 */
public class ServerAction extends Thread{
    private static final String TAG = ServerAction.class.getSimpleName();
    private static Client client;
    ServerSocket serverSocket = null;

//    private void initSocket() {
//        ServerSocket serverSocket = null;
//        try {
//            serverSocket = new ServerSocket(Config.PORT);
//            Socket socket=serverSocket.accept();
//            while (true) {
//                client = new Client(socket);
//            }
//
//        } catch (IOException e) {
//            e.printStackTrace();
//            closeSocket();
//        }
//
//    }

    @Override
    public void run() {
        try {
            serverSocket = new ServerSocket(Config.PORT);
        } catch (IOException e) {
            e.printStackTrace();
        }

        while (true) {
            try {
                Socket socket=serverSocket.accept();
                client = new Client(socket);
                DataPackage dtpg = (DataPackage) client.receive();
                Object data = dtpg.getData();
                LogUtils.e("LoginActivity2-receive", "receive");

                switch (dtpg.getType()) {
                    case LOGIN:
//                        EventBus.getDefault().post(new LoginEvent(EventConfig.LGOIN));
                        break;
                    case POINT:
                        EventBus.getDefault().post(new BoardEvent(EventConfig.POINT,data));
                        break;
                    case COMMAND:
                        EventBus.getDefault().post(new BoardEvent(EventConfig.COMMAND,data));
                        break;
                    case COLOR:
                        EventBus.getDefault().post(new BoardEvent(EventConfig.COLOR,data));
                        break;
                    case STROKE:
                        EventBus.getDefault().post(new BoardEvent(EventConfig.STROKE,data));
                        break;
                }


            } catch (ClassNotFoundException e) {
                e.printStackTrace();
                closeSocket();
            } catch (IOException e) {
                e.printStackTrace();
                closeSocket();
            }
        }
    }

    public static void sendData(DataPackage.DataType type, Object data) {
        DataPackage dp = new DataPackage(type, data);
        sendData(dp);
    }
    public static void sendData(DataPackage dp) {
        if (client != null && !client.isClosed()) {
            try {
                client.send(dp);
            } catch (IOException e) {
                closeSocket();
            }
        }
    }
    public static void closeSocket() {
        if(client!=null) {
            client.close();
            System.out.println(TAG+": server close");
        }
    }
}
